﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aoxuelf.MDSTool.Model;

namespace Aoxuelf.MDSTool.SQL
{
    public class DALQueryMethodWriter : MethodWriter
    {
        public DALQueryMethodWriter(DALQueryMethodInfo methodWriteInfo)
            : base(methodWriteInfo)
        {
        }

        private bool IsPage
        {
            get { return ((DALQueryMethodInfo)this.methodWriteInfo).IsPage; }
        }

        protected override void WriteSQLParams()
        {
            if (methodWriteInfo.IsTransaction)
            {
                WriteSQLParam(SQLParamInfo.DBTran);
            }
            base.WriteSQLParams();
            if (IsPage)
            {
                WriteSQLParam(SQLParamInfo.PageSize);
                WriteSQLParam(SQLParamInfo.PageNO);
            }
        }

        protected override void WriteMethodSig()
        {
            string format_Tran = "public {0} {1}(DbTransaction tran, IDictionary<string, string> dicParam)";
            string format = "public {0} {1}(IDictionary<string, string> dicParam)";

            if (IsPage)
            {
                if (methodWriteInfo.IsTransaction)
                {
                    builder.AppendLine(string.Format(format_Tran, "IDictionary<string, object>", methodWriteInfo.MethodName));
                }
                else
                {
                    builder.AppendLine(string.Format(format, "IDictionary<string, object>", methodWriteInfo.MethodName));
                }
            }
            else
            {
                if (methodWriteInfo.IsTransaction)
                {
                    builder.AppendLine(string.Format(format_Tran, "DataTable", methodWriteInfo.MethodName));
                }
                else
                {
                    builder.AppendLine(string.Format(format, "DataTable", methodWriteInfo.MethodName));
                }
            }
        }

        protected override void WriteMethodBody()
        {
            builder.AppendLine("string sqlPath = \"\";");
            if (IsPage)
            {
                if (methodWriteInfo.IsTransaction)
                {
                    builder.AppendLine("return DBHelper.QueryPageData(sqlPath, dicParam);");
                }
                else
                {
                    builder.AppendLine("return DBHelper.QueryPageData(sqlPath, dicParam);");
                }
            }
            else
            {
                if (methodWriteInfo.IsTransaction)
                {
                    builder.AppendLine("return DBHelper.QueryDataFromConfig(sqlPath, dicParam, tran);");
                }
                else
                {
                    builder.AppendLine("return DBHelper.QueryDataFromConfig(sqlPath, dicParam);");
                }
            }
        }

        protected override void WriteReturnBody()
        {
            if (IsPage)
            {
                builder.AppendLine("/// <content type=\"DataTable\" name=\"#DT_RESULT#\">");
                base.WriteColumns();
                builder.AppendLine("/// </content>");
                WriteContent(ReturnContent.Page_TotalCount);
                WriteContent(ReturnContent.Page_TotalPage);
                WriteContent(ReturnContent.Exe_Flag);
                WriteContent(ReturnContent.Exe_UserMsg);
            }
            else
            {
                base.WriteReturnBody();
            }
        }
    }
}
